<template>
  <div class="sg-overview">
    <div class="page-header">
      <h1>人才培养总览</h1>
      <div>
        <el-button type="success" @click="exportData">
          <el-icon><Download /></el-icon>
          导出数据
        </el-button>
        <el-button type="primary" @click="fetchAll" :loading="loading">刷新</el-button>
      </div>
    </div>

    <el-card class="filter-card">
      <div class="filters">
        <el-input v-model="q.teacherName" placeholder="教师姓名" clearable style="width: 140px;" />
        <el-select v-model="q.studentType" placeholder="学生类型" clearable style="width: 140px;">
          <el-option label="本科生" value="本科生" />
          <el-option label="硕士生" value="硕士生" />
          <el-option label="博士生" value="博士生" />
        </el-select>
        <el-select v-model="q.achievementType" placeholder="成果类型" clearable style="width: 140px;">
          <el-option label="竞赛" value="获奖" />
          <el-option label="项目" value="项目" />
          <el-option label="论文" value="论文" />
        </el-select>
        <el-input v-model="q.level" placeholder="级别/分区" clearable style="width: 160px;" />
        <el-input v-model="q.year" placeholder="年份" clearable style="width: 100px;" />
        <el-button type="primary" :loading="loading" style="margin-left: 12px;" @click="fetchAll">查询</el-button>
      </div>
    </el-card>

    <el-row :gutter="12" class="stats-row">
      <el-col :span="6"><el-card class="stat-card"><div class="stat-num">{{ stats.totalGuidance }}</div><div class="stat-label">指导记录</div></el-card></el-col>
      <el-col :span="6"><el-card class="stat-card"><div class="stat-num">{{ stats.totalAchievements }}</div><div class="stat-label">成果总数</div></el-card></el-col>
      <el-col :span="6"><el-card class="stat-card"><div class="stat-num">{{ stats.awardCount }}</div><div class="stat-label">获奖</div></el-card></el-col>
      <el-col :span="6"><el-card class="stat-card"><div class="stat-num">{{ stats.paperCount }}</div><div class="stat-label">论文</div></el-card></el-col>
    </el-row>

    <el-card>
      <el-table :data="rows" stripe v-loading="loading">
        <el-table-column prop="user.name" label="教师" width="120">
          <template #default="{ row }">{{ row.user?.name || '-' }}</template>
        </el-table-column>
        <el-table-column prop="studentName" label="学生姓名" width="120" />
        <el-table-column prop="studentType" label="学生类型" width="100" />
        <el-table-column prop="guidanceCategory" label="指导内容" width="120" />
        <el-table-column label="起止" width="220">
          <template #default="{ row }">{{ d(row.startDate) }} ~ {{ d(row.endDate) }}</template>
        </el-table-column>
        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }"><el-tag :type="tag(row.status)">{{ st(row.status) }}</el-tag></template>
        </el-table-column>
      </el-table>
      <div style="text-align:right;margin-top:12px;">
        <el-pagination background layout="prev, pager, next, jumper" :page-size="pageSize" :current-page="page" :total="total" @current-change="onPage" />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { Download } from '@element-plus/icons-vue'
import studentGuidanceApi from '@/api/studentGuidance'

const q = ref({ teacherName: '', studentType: '', achievementType: '', level: '', year: '' })
const page = ref(1)
const pageSize = ref(10)
const total = ref(0)
const rows = ref([])
const loading = ref(false)
const stats = ref({ totalGuidance: 0, totalAchievements: 0, awardCount: 0, paperCount: 0, projectCount: 0, excellentThesisCount: 0 })

function params(){
  return {
    page: page.value, size: pageSize.value,
    teacherName: q.value.teacherName || undefined,
    studentType: q.value.studentType || undefined,
    achievementType: q.value.achievementType || undefined,
    level: q.value.level || undefined,
    year: q.value.year || undefined
  }
}

function d(v){ return String(v||'').substring(0,10) }
function st(s){ if(!s) return '待审核'; if(s==='APPROVED') return '已通过'; if(s==='REJECTED') return '已驳回'; return '待审核' }
function tag(s){ const t=st(s); return t==='已通过'?'success':(t==='已驳回'?'danger':'info') }

async function fetchAll(){
  try{ loading.value=true; const res = await studentGuidanceApi.list(params()); rows.value = res?.content || []; total.value = res?.totalElements || 0; const s = await studentGuidanceApi.stats(q.value); stats.value = s || stats.value } finally { loading.value=false }
}
function onPage(p){ page.value=p; fetchAll() }

const exportData = async () => {
  const { exportData: exportUtil } = await import('@/utils/exportUtils')
  
  const filters = {
    teacherName: q.value.teacherName,
    type: q.value.achievementType
  }
  
  await exportUtil('student-guidance', filters, '人才培养列表.xlsx')
}

onMounted(fetchAll)
</script>

<style scoped>
.sg-overview { max-width: 1400px; }
.page-header { display:flex; justify-content:space-between; align-items:center; margin-bottom:16px; }
.filters { display:flex; align-items:center; flex-wrap:wrap; gap:12px; }
.stats-row { margin-bottom: 16px; }
.stat-card { text-align:center; }
.stat-num { font-size:24px; font-weight:600; color:#409eff; }
.stat-label { color:#666; }
</style> 